<?php

ob_start();
include_once ('../../utilities.php')

?>
<!DOCTYPE html>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="../../reset.css">
	<link rel="stylesheet" type="text/css" href="../../mainStyle.css">
	<link rel="stylesheet" type="text/css" href="../../searchButton.css">
	<title>Notifications</title>
</head>
<body>

	<div id="container">
		<?php

$root = '../../';
include_once ('../../header.php')

?>
        
		<div id="page-body">
		<?php

if (isset($_POST['submit']))
{ //If fields are set from form
    $myuser = unserialize($_SESSION['User']);
    if ($myuser->getPermissions() == 6)
    {
        //User is Master Admin
        $accept = intval($_POST['group1']);
        //If the admin accepts this chapter
        if ($accept)
        {
            $selectedOption = intval($_POST['Chapters']);
            //Get notification info
            $query = "SELECT * FROM Notifications WHERE NotificationID = " . $selectedOption;
            $data = $mysqli->query($query);
            if ($data->num_rows != 0)
            {
                while ($arr = $data->fetch_array(MYSQLI_ASSOC))
                {
                    //Insert the chapter
                    $query2 = "INSERT INTO Chapters (ChapterName,RegionID,Description) VALUES('" . $arr['ChapterName'] .
                        "', '" . $arr['RegionID'] . "', '" . $arr['Description'] . "')";
                    $registerquery = $mysqli->query($query2);
                }
            }
        }
        //Delete the notification
        $selectedOption = intval($_POST['Chapters']);
        $query = "DELETE FROM Notifications WHERE NotificationID = " . $selectedOption;
        $registerquery = $mysqli->query($query);
    }
}
if (!empty($_SESSION['LoggedIn']) && !empty($_SESSION['User']))
{ //Form to input info if logged in
    $myuser = unserialize($_SESSION['User']);
    if ($myuser->getPermissions() == 6)
    {

?>
				<p>Review chapter applications and accept or reject them</p>
				
				<form method="POST" action="./">
					<select name="Chapters" id = "Chapters">
					<?php

        //Show every notification, send id as value
        $query = "SELECT * FROM Notifications";
        $data = $mysqli->query($query);
        if ($data->num_rows != 0)
        {
            while ($arr = $data->fetch_array(MYSQLI_ASSOC))
            {

?>
								<option value="<?php

                $arr["NotificationID"];

?>"> Chapter Name: <?php

                echo $arr["ChapterName"]

?>. User registering chapter: <?php

                echo $arr["PresidentName"]

?>. </option>
								<?php

            }
        }

?>
					</select><br>
					<input type="radio" name="group1" value="1" checked> Accept <br>
					<input type="radio" name="group1" value="0"> Reject <br>
					<input type="submit" name="submit" id="submit" value="Register" />
				</form>
			
			<?php

    } else
    {
        echo "<p> You do not have permission to view this page.</p>";
    }
} else
{

?>
				
		   <p>You must login to view this page.</p>
								
		   <?php

}

?>
		</div>
		
		<?php

include_once ('../../footer.php')

?>
	</div>

</body>
</html>